package com.gewu.pm.ui.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.gewu.pm.R;
import com.gewu.pm.app.AppAdapter;
import com.hjq.base.BaseAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * 筛选页面 - 订单状态适配器（多选）
 */
public final class FilterOrderStatusAdapter extends AppAdapter<String> {

    /** 选中的位置列表 */
    private List<Integer> selectedPositions = new ArrayList<>();

    public FilterOrderStatusAdapter(Context context) {
        super(context);
    }

    @NonNull
    @Override
    public BaseAdapter<?>.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ViewHolder();
    }

    private final class ViewHolder extends AppAdapter<?>.ViewHolder {

        private final TextView tvStatus;
        private final ImageView ivMarker;

        private ViewHolder() {
            super(R.layout.filter_order_status_item);
            tvStatus = findViewById(R.id.tv_status);
            ivMarker = findViewById(R.id.iv_marker);
        }

        @Override
        public void onBindView(int position) {
            String statusText = getItem(position);
            tvStatus.setText(statusText);
            
            // 设置选中状态
            boolean isSelected = selectedPositions.contains(position);
            tvStatus.setSelected(isSelected);
            
            // 显示或隐藏角标
            ivMarker.setVisibility(isSelected ? View.VISIBLE : View.INVISIBLE);
        }
    }

    /**
     * 切换选中状态
     * 
     * @param position 要切换的位置
     */
    public void toggleSelection(int position) {
        if (selectedPositions.contains(position)) {
            // 取消选中
            selectedPositions.remove(Integer.valueOf(position));
        } else {
            // 选中
            selectedPositions.add(position);
        }
        
        // 刷新该item
        notifyItemChanged(position);
    }

    /**
     * 获取所有选中的位置
     */
    public List<Integer> getSelectedPositions() {
        return new ArrayList<>(selectedPositions);
    }

    /**
     * 获取所有选中的文字（逗号分隔）
     */
    public String getSelectedText() {
        if (selectedPositions.isEmpty()) {
            return "";
        }
        
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < selectedPositions.size(); i++) {
            int position = selectedPositions.get(i);
            if (position >= 0 && position < getCount()) {
                if (i > 0) {
                    sb.append("、");
                }
                sb.append(getItem(position));
            }
        }
        return sb.toString();
    }

    /**
     * 清除所有选中状态
     */
    public void clearSelection() {
        List<Integer> oldPositions = new ArrayList<>(selectedPositions);
        selectedPositions.clear();
        
        // 刷新所有之前选中的item
        for (int position : oldPositions) {
            if (position >= 0 && position < getCount()) {
                notifyItemChanged(position);
            }
        }
    }

    /**
     * 设置选中的位置列表
     */
    public void setSelectedPositions(List<Integer> positions) {
        // 清除旧的选中状态
        List<Integer> oldPositions = new ArrayList<>(selectedPositions);
        selectedPositions.clear();
        selectedPositions.addAll(positions);
        
        // 刷新所有变化的item
        for (int position : oldPositions) {
            if (!selectedPositions.contains(position)) {
                notifyItemChanged(position);
            }
        }
        for (int position : selectedPositions) {
            if (!oldPositions.contains(position)) {
                notifyItemChanged(position);
            }
        }
    }
}

